package com.wiretrack.circuit;

import java.awt.Color;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

public class ScreenFlasher extends Thread
{
	private static Logger log = Logger.getLogger(ScreenFlasher.class.getName());
	
	private JPanel panelOne;
	private JPanel panelTwo;
	private Color originalColor = new Color(238, 238, 238);
	private Color[] colors;
	private ScreenFlash screenFlash;
	private int timesToFlash;
	private int flashDelayTime;

	public ScreenFlasher(ScreenFlash sf, JPanel one, JPanel two, Color endingColor)
	{
		panelOne = one;
		panelTwo = two;
		screenFlash = sf;
		colors = sf.getColors();
		timesToFlash = sf.times;
		flashDelayTime = sf.lengthInMs;
		originalColor = endingColor;
	}

	public ScreenFlasher(ScreenFlasher colorChangerGetRidOfThisConstructorEventually)
	{
		panelOne = colorChangerGetRidOfThisConstructorEventually.panelOne;
		panelTwo = colorChangerGetRidOfThisConstructorEventually.panelTwo;
		colors = colorChangerGetRidOfThisConstructorEventually.colors;
		timesToFlash = 20;
		flashDelayTime = 30;
	}

	public void run()
	{
		if ( panelOne != null && panelTwo != null )
		{
			for (int i = 0; i < timesToFlash; i++)
			{
				for (int j = 0; j < colors.length; j++)
				{
					panelOne.setBackground(colors[j]);
					panelTwo.setBackground(colors[j]);
					sleepForABit();
				}
			}

			panelOne.setBackground(originalColor);
			panelTwo.setBackground(originalColor);
		}
		else
		{
			log.warn("Flasher not run - no panels to paint to for: " + screenFlash.id);
		}
	}

	private void sleepForABit()
	{
		try
		{
			sleep(flashDelayTime);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
}
